package bookmanage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JCheckBox;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MemberSelect extends JInternalFrame {
	private JComboBox comboBox;
	private JComboBox comboBox1;
	private JScrollPane scrollPane;
	private JTable table;
	private JButton btnNewButton;
	private JTextField searchTF;
	private JLabel result;
	private JCheckBox selectAll;

	Connection con;
	BMMain main;
	DefaultTableModel model = new DefaultTableModel();
	Vector<String> colName = new Vector<String>();
	SimpleDateFormat sdf = new SimpleDateFormat("yy/MM/dd");

	public MemberSelect(BMMain main) {
		super("회원 조회",true,true,false,true);
		this.con=main.con;
		this.main=main;
		colName.add("아이디");
		colName.add("이름");
		colName.add("성별");
		colName.add("생일");
		colName.add("폰번호");
		colName.add("이메일");
		colName.add("가입일");
		colName.add("회원등급");

		setResizable(false);
		setBounds(100, 100, 650, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getComboBox());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getSearchTF());
		getContentPane().add(getResult());
		getContentPane().add(getSelectAll());
		
		JTableHeader jh = table.getTableHeader();
		jh.setReorderingAllowed(false);
		
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.addItem("아이디");
			comboBox.addItem("이름");
			comboBox.addItem("폰 번호");
			comboBox.addItem("회원 등급");
			comboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent e) {
					String str = (String)comboBox.getSelectedItem();
					if(searchTF != null) getContentPane().remove(searchTF);
					if(comboBox1 != null){
						comboBox1.removeAllItems();
						getContentPane().remove(comboBox1);
					}
					if(str.equals("아이디") || str.equals("이름") || str.equals("폰 번호")){
						searchTF = new JTextField();
						searchTF.setBounds(125, 10, 116, 22);
						searchTF.setColumns(10);
						getContentPane().add(searchTF);
					}else if(str.equals("회원 등급")){
						comboBox1 = new JComboBox<>();
						comboBox1.setBounds(125, 10, 116, 22);
						comboBox1.addItem("메니져");
						comboBox1.addItem("강사");
						comboBox1.addItem("학생");
						getContentPane().add(comboBox1);
					}
					MemberSelect.this.updateUI();
				}
			});
			comboBox.setBounds(12, 10, 101, 23);
		}
		return comboBox;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(10, 37, 594, 224);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			model = new DefaultTableModel(colName, 0) {
				// 셀 내용 수정 불가
				public boolean isCellEditable(int rowIndex, int mColIndex) {
					return false;
				}
			};
			table = new JTable(model);
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					int idxrow = table.getSelectedRow();
					String m_id = (String)table.getValueAt(idxrow, 0);
					if(main.med!=null){
						main.med.setVisible(true);
						main.med.selectMember(m_id);
					}else{
						main.med = new MemberEditDel(main);
						main.contentPane.add(main.med);
						main.med.setBounds(50, 50, 500, 550);
						main.med.setVisible(true);
						main.med.selectMember(m_id);
					}
				}
			});
		}
		return table;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int cnt = comboBox.getSelectedIndex();
					String search="";
					String gra="";
					String where="";
					if(selectAll.isSelected()==false){
						if(cnt==0 || cnt==1 || cnt==2){
							search=searchTF.getText().trim();
							if(search.equals("")){
								JOptionPane.showMessageDialog(null, "검색어를 입력해주세요!");
								searchTF.requestFocus();
								return;
							}
							if(cnt==0) where="where m_id='"+search+"'";
							if(cnt==1) where="where m_name='"+search+"'";
							if(cnt==2) where="where m_phone like '%"+search+"%'";
						}else{
							gra=(String)comboBox1.getSelectedItem();
							where="where m_grade="+gra;
						}
					}
					model.setRowCount(0);
					try {
						String sql="select m_id,m_name,m_gender,m_birth,m_phone,m_email,m_joindate,m_grade from membertbl "+where;
						System.out.println(sql);
						PreparedStatement psmt=con.prepareStatement(sql);
						ResultSet rs = psmt.executeQuery();
						while(rs.next()){
							Vector<String> vec = new Vector<String>();
							vec.add(rs.getString(1));
							vec.add(rs.getString(2));
							vec.add(rs.getString(3));
							vec.add(sdf.format(rs.getDate(4)));
							vec.add(rs.getString(5));
							vec.add(rs.getString(6));
							vec.add(sdf.format(rs.getDate(7)));
							vec.add(rs.getString(8));
							model.addRow(vec);
						}
						if(model.getRowCount()==0){
							JOptionPane.showMessageDialog(null, "검색된 데이타가 없습니다!");
						}
						result.setText(model.getRowCount()+"건 검색되었습니다.");
					} catch (Exception e2) {
						e2.printStackTrace();
					}

				}
			});
			btnNewButton.setBounds(325, 9, 97, 23);
		}
		return btnNewButton;
	}
	private JTextField getSearchTF() {
		if (searchTF == null) {
			searchTF = new JTextField();
			searchTF.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode()==KeyEvent.VK_ENTER){
						int cnt = comboBox.getSelectedIndex();
						String search="";
						String gra="";
						String where="";
						if(selectAll.isSelected()==false){
							if(cnt==0 || cnt==1 || cnt==2){
								search=searchTF.getText().trim();
								if(search.equals("")){
									JOptionPane.showMessageDialog(null, "검색어를 입력해주세요!");
									searchTF.requestFocus();
									return;
								}
								if(cnt==0) where="where m_id='"+search+"'";
								if(cnt==1) where="where m_name='"+search+"'";
								if(cnt==2) where="where m_phone like '%"+search+"%'";
							}
						}
						model.setRowCount(0);
						try {
							String sql="select m_id,m_name,m_gender,m_birth,m_phone,m_email,m_joindate,m_grade from membertbl "+where;
							System.out.println(sql);
							PreparedStatement psmt=con.prepareStatement(sql);
							ResultSet rs = psmt.executeQuery();
							while(rs.next()){
								Vector<String> vec = new Vector<String>();
								vec.add(rs.getString(1));
								vec.add(rs.getString(2));
								vec.add(rs.getString(3));
								vec.add(sdf.format(rs.getDate(4)));
								vec.add(rs.getString(5));
								vec.add(rs.getString(6));
								vec.add(sdf.format(rs.getDate(7)));
								vec.add(rs.getString(8));
								model.addRow(vec);
							}
							if(model.getRowCount()==0){
								JOptionPane.showMessageDialog(null, "검색된 데이타가 없습니다!");
							}
							result.setText(model.getRowCount()+"건 검색되었습니다.");
						} catch (Exception e2) {
							e2.printStackTrace();
						}
					}
				}
			});
			searchTF.setBounds(125, 10, 116, 22);
			searchTF.setColumns(10);
		}
		return searchTF;
	}
	private JLabel getResult() {
		if (result == null) {
			result = new JLabel("");
			result.setBounds(434, 10, 170, 23);
		}
		return result;
	}
	private JCheckBox getSelectAll() {
		if (selectAll == null) {
			selectAll = new JCheckBox("전체");
			selectAll.setBounds(265, 10, 60, 23);
		}
		return selectAll;
	}
}
